package com.huali.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "CookieServlet", value = "/Cookie")
public class CookieServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应编码
        response.setContentType("text/html;charset=UTF-8");
        //获取所有的cookie
        Cookie[] cookies = request.getCookies();
        //inn(判断对象不为空（null）) ifn(判断对象为空（null）)
        if (cookies != null) {
            //iter 快速生成
            for (Cookie cookie : cookies) {
                System.out.println("cookie的键为：" + cookie.getName() + "cookie的值为：" + cookie.getValue());
            }
        }
        //获取session数据
        HttpSession session = request.getSession();
        //从session中获取数据
        Object user = session.getAttribute("user");
        if (user != null) {
            System.out.println("session中的user数据:" + user);
        }
        //向页面进行响应
        response.getWriter().write("服务器响应成功！！");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
